package com.bwie.gddproject.index.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bwie.gddproject.R;
import com.bwie.gddproject.event.CidEvent;
import com.bwie.gddproject.index.adapter.Classify1Adapter;
import com.bwie.gddproject.index.adapter.RightAdapter;
import com.bwie.gddproject.index.model.bean.Classify2Bean;
import com.bwie.gddproject.index.model.bean.VpBean;
import com.bwie.gddproject.index.precenter.ClassifyPrecenter;
import com.bwie.gddproject.index.precenter.IClassifyPrecenter;
import com.bwie.gddproject.index.view.XiaoXiActivity;
import com.bwie.gddproject.login.view.LoginActivity;
import com.bwie.gddproject.search.view.SearchActivity;
import com.bwie.gddproject.utils.GlideImageLoader;
import com.youth.banner.Banner;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 我走路带风 on 2017/11/9.
 */

public class ClassifyFragment extends Fragment implements IClassifyFragment, View.OnClickListener {
    private View view;
    private RecyclerView mFlRv1;
    private Banner mFlBanner;
    private RecyclerView mFlRv2;
    private IClassifyPrecenter classifyPrecenter;
    private LinearLayout mSaoyisao;
    /**
     * 笔记本电脑
     */
    private EditText mEt;
    private RelativeLayout mSousuo;
    private LinearLayout mXiaoxi;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.classify_fragment, null);
        initView(view);
        EventBus.getDefault().register(this);
        classifyPrecenter = new ClassifyPrecenter(this);
        classifyPrecenter.getVpData();
        classifyPrecenter.getVpData2("1");
        return view;
    }

    private void initView(View view) {
        mFlRv1 = (RecyclerView) view.findViewById(R.id.fl_rv1);
        mFlBanner = (Banner) view.findViewById(R.id.fl_banner);
        mFlRv2 = (RecyclerView) view.findViewById(R.id.fl_rv2);
        mFlRv1.setOnClickListener(this);
        mFlBanner.setOnClickListener(this);
        mFlRv2.setOnClickListener(this);
        mSaoyisao = (LinearLayout) view.findViewById(R.id.saoyisao);
        mEt = (EditText) view.findViewById(R.id.et);
        mSousuo = (RelativeLayout) view.findViewById(R.id.sousuo);
        mXiaoxi = (LinearLayout) view.findViewById(R.id.xiaoxi);
        mXiaoxi.setOnClickListener(this);
        mSaoyisao.setOnClickListener(this);
        mEt.setOnClickListener(this);
        mSousuo.setOnClickListener(this);
    }


    @Override
    public void showClassify1(VpBean vpBean) {
        List<VpBean.DataBean> data = vpBean.getData();
        Classify1Adapter classify1Adapter = new Classify1Adapter(this, data);
        mFlRv1.setLayoutManager(new LinearLayoutManager(getActivity()));
        mFlRv1.setAdapter(classify1Adapter);
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            String icon = data.get(i).getIcon();
            strings.add(icon);
        }
        mFlBanner.setImages(strings);
        mFlBanner.isAutoPlay(true);
        mFlBanner.setDelayTime(3000);
        mFlBanner.setImageLoader(new GlideImageLoader());
        mFlBanner.start();
    }

    @Override
    public void toast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showClassify2(Classify2Bean classify2Bean) {
        List<Classify2Bean.DataBean> data = classify2Bean.getData();
        RightAdapter rightAdapter = new RightAdapter(getActivity(), data);
        mFlRv2.setLayoutManager(new LinearLayoutManager(getActivity()));
        mFlRv2.setAdapter(rightAdapter);
    }

    @Subscribe
    public void getCid(CidEvent cidEvent) {
        classifyPrecenter.getVpData2(cidEvent.getCid());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.fl_rv1:
                break;
            case R.id.fl_banner:
                break;
            case R.id.fl_rv2:
                break;
            case R.id.xiaoxi:
                SharedPreferences user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                if (user.getString("name", "") == null || user.getString("name", "") == "") {
                    Toast.makeText(getActivity(), "请登录您的账号...", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(intent,1);
                } else {
                    Intent intent = new Intent(getActivity(), XiaoXiActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.saoyisao:
                break;
            case R.id.et:
                break;
            case R.id.sousuo:
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
        }
    }
}
